﻿using BizControl.Library.BusinessObjects;
using Csla.Xaml;
using BizControl.Library.BusinessObjects.Activities;
using BizControl.Presentation.Infrastructure.Controls;
using Microsoft.Practices.Prism.Regions;
using System.ComponentModel.Composition;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism;
using System;
using BizControl.Presentation.Infrastructure.Constants;
using System.Windows.Input;
using Microsoft.Practices.Prism.Modularity;
using System.ComponentModel.Composition.Hosting;
using Telerik.Windows.Controls.ScheduleView;
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace BizControl.Presentation.ActivitiesModule.ViewModels
{
  [Export]
  public class ActivitiesViewModel : ViewModel<ActivityList>, INavigationAware, IRegionMemberLifetime, IPartImportsSatisfiedNotification
  {
    private readonly DelegateCommand<Guid?> _editActivityCommand;
    private readonly DelegateCommand _refreshListCommand;
    private readonly DelegateCommand _addActivityCommand;
    private readonly DelegateCommand _copyCommand;

    private readonly DelegateCommand _goBackCommand;
    private IRegionNavigationService navigationService;

    private IRegionManager _regionManager;

    [ImportingConstructor]
    public ActivitiesViewModel(IRegionManager regionManager/*, CompositionContainer container*/)
    {
      _regionManager = regionManager;
      _editActivityCommand = new DelegateCommand<Guid?>(EditActivity);
      _refreshListCommand = new DelegateCommand(RefreshList);
      _addActivityCommand = new DelegateCommand(AddActivity);
      _copyCommand = new DelegateCommand(CopyActivity, CanCopyActivity);

      _goBackCommand = new DelegateCommand(GoBack, CanGoBack);
      RefreshList();
    }

    private void CopyActivity()
    {
      if (SelectedActivity != null)
      {
        var uriQuery = new UriQuery();
        uriQuery.Add(NavigationParametersNames.CopyActivityName, NavigationParametersNames.CopyActivity);
        uriQuery.Add(NavigationParametersNames.SourceActivityIdName, SelectedActivity.Id.ToString());
        var uri = new Uri(ViewNames.ActivityEditViewName + uriQuery.ToString(), UriKind.Relative);
        this._regionManager.RequestNavigate(ShellRegionNames.MainContentRegion, uri);
      }
    }

    private bool CanCopyActivity()
    {
      return SelectedActivity != null && !SelectedActivity.IsEmpty();
    }

    // GoBack test
    private void GoBack()
    {
      if (navigationService.Journal.CanGoBack)
      {
        navigationService.Journal.GoBack();
      }
    }

    private bool CanGoBack()
    {
      if (navigationService != null)
        return navigationService.Journal.CanGoBack;
      return false;
    }

    public ICommand GoBackCommand
    {
      get { return this._goBackCommand; }
    }
    // end GoBack

    #region Commands

    public ICommand EditActivityCommand
    {
      get { return this._editActivityCommand; }
    }

    public ICommand AddActivityCommand
    {
      get { return this._addActivityCommand; }
    }

    public ICommand CopyCommand
    {
      get { return this._copyCommand; }
    }

    private ActivityInfo _selectedActivity;
    public ActivityInfo SelectedActivity
    {
      get { return _selectedActivity; }
      set
      {
        _selectedActivity = value;
        OnPropertyChanged("SelectedActivity");
        _copyCommand.RaiseCanExecuteChanged();
      }
    }

    public ObservableCollection<Appointment> Appointments
    {
      get;
      set;
    }

    private void RefreshList()
    {
      DoRefresh(GetList);
      FillAppointments();
    }

    private void FillAppointments()
    {
      Appointments = new ObservableCollection<Appointment>();
      foreach (var activity in Model)
      {
        Appointment appointment = new Appointment()
        {
          Subject = activity.ShortenDescription,
          Start = activity.StartDate ?? activity.CreatedOn,
          End = activity.DueDate ?? DateTime.MaxValue
        };
        Appointments.Add(appointment);
      }
    }

    public ICommand RefreshListCommand
    {
      get { return this._refreshListCommand; }
    }

    private void EditActivity(Guid? activityId)
    {
      if (!string.IsNullOrEmpty(activityId.ToString()))
      {
        var uriQuery = new UriQuery();
        uriQuery.Add(NavigationParametersNames.IdName, activityId.ToString());
        var uri = new Uri(ViewNames.ActivityEditViewName + uriQuery.ToString(), UriKind.Relative);
        this._regionManager.RequestNavigate(ShellRegionNames.MainContentRegion, uri);
      }
    }

    private void AddActivity()
    {
      var uriQuery = new UriQuery();
      uriQuery.Add(NavigationParametersNames.NewActivityName, NavigationParametersNames.CreateNewActivity);
      var uri = new Uri(ViewNames.ActivityEditViewName + uriQuery.ToString(), UriKind.Relative);
      this._regionManager.RequestNavigate(ShellRegionNames.MainContentRegion, uri);
    }

    #endregion

    ActivityList GetList()
    {
      return ActivityList.GetActivityList();
    }

    #region INavigationAware Members

    public bool IsNavigationTarget(NavigationContext navigationContext)
    {
      return true;
    }

    public void OnNavigatedFrom(NavigationContext navigationContext)
    {
    }

    public void OnNavigatedTo(NavigationContext navigationContext)
    {
      navigationService = navigationContext.NavigationService;
      RefreshList();
      _goBackCommand.RaiseCanExecuteChanged();
    }

    #endregion

    #region IRegionMemberLifetime Members

    public bool KeepAlive
    {
      get { return true; }
    }

    #endregion

    #region IPartImportsSatisfiedNotification Members

    public void OnImportsSatisfied()
    {
    }

    #endregion
  }
}
